package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"teacher-helper/global"
	"teacher-helper/models"
	"teacher-helper/models/request"
	"teacher-helper/service"
)

func GetDepartmentList(c *gin.Context) {
	var queryInfo request.QueryInfo
	if err := c.ShouldBind(&queryInfo); err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
		return
	}
	departments, total, err := service.GetDepartmentList(queryInfo)
	if err != nil {
		c.JSON(202, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "获取院系列表成功", "data": map[string]interface{}{
			"departments": departments,
			"total": total,
		}})
	}
	return
}

func GetAllDepartmentTree(c *gin.Context){
	depart_tree, err := service.GetAllDepartmentTree()
	if err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "获取菜单树成功", "data": depart_tree})
	}
	return
}

func AddOneDepart(c *gin.Context)  {
	var department models.SysDepartment
	if err := c.BindJSON(&department);err != nil{
		global.Logger.Error(err.Error())
		c.JSON(201, gin.H{"msg": err})
		return
	}
	err := service.AddOneDepartment(department)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg": "添加院系成功"})
	}
	return
}

func UpdateDepartment(c *gin.Context) {
	var department models.SysDepartment
	if err := c.BindJSON(&department); err != nil {
		c.JSON(201, gin.H{"msg": "参数获取失败"})
		return
	}
	err := service.UpdateDepartment(department)
	if err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "修改院系成功", "data": nil})
	}
	return
}

func DeleteDepartment(c *gin.Context) {
	id, ok := c.GetQuery("id")
	depart_id, err := strconv.Atoi(id)
	if !ok || err != nil {
		c.JSON(201, gin.H{"msg": "参数获取失败"})
		return
	}
	err = service.DeleteDepartment(depart_id)
	if err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "删除院系成功", "data": nil})
	}
	return
}

//本科生专业
//公共事业管理（卫生事业管理方向）
//公共事业管理（健康产业管理方向）
//信息管理与信息系统（医药方向）
//劳动与社会保障

//社会医学与卫生事业管理
//公共卫生专业硕士（MPH）
//图书情报专业硕士（MLIS）